package com.ruoyi.asset.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.asset.mapper.AssetStockDetailMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AssetStockMapper;
import com.ruoyi.asset.domain.AssetStock;
import com.ruoyi.asset.service.IAssetStockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 盘点任务Service业务层处理
 * 
 * @author asset
 * @date 2024-12-25
 */
@Service
public class AssetStockServiceImpl implements IAssetStockService 
{
    @Autowired
    private AssetStockMapper assetStockMapper;

    @Autowired
    private AssetStockDetailMapper assetStockDetailMapper;

    /**
     * 查询盘点任务
     * 
     * @param id 盘点任务主键
     * @return 盘点任务
     */
    @Override
    public AssetStock selectAssetStockById(Long id)
    {
        return assetStockMapper.selectAssetStockById(id);
    }

    /**
     * 查询盘点任务列表
     * 
     * @param assetStock 盘点任务
     * @return 盘点任务
     */
    @Override
    public List<AssetStock> selectAssetStockList(AssetStock assetStock)
    {
        return assetStockMapper.selectAssetStockList(assetStock);
    }

    /**
     * 新增盘点任务
     * 
     * @param assetStock 盘点任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAssetStock(AssetStock assetStock)
    {
        if(assetStockMapper.countUnDoneAssetStock() > 0){
            throw new ServiceException("还有未开始或进行中的盘点任务，请先完成盘点或者将任务设置为暂停状态");
        }
        assetStock.setCreateTime(DateUtils.getNowDate());
        int ret = assetStockMapper.insertAssetStock(assetStock);
        assetStockDetailMapper.copyAssetStockDetail(assetStock.getId());
        return ret;
    }

    /**
     * 修改盘点任务
     * 
     * @param assetStock 盘点任务
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAssetStock(AssetStock assetStock)
    {
        if(assetStock.getStatus().equalsIgnoreCase("1")){
            int ret = assetStockDetailMapper.countUnStockAsset(assetStock.getId());
            if(ret > 0){
                throw new ServiceException("本次任务还有 "+ret+" 个资产没有盘点");
            }
        }
        assetStock.setUpdateTime(DateUtils.getNowDate());
        return assetStockMapper.updateAssetStock(assetStock);
    }

    /**
     * 批量删除盘点任务
     * 
     * @param ids 需要删除的盘点任务主键
     * @return 结果
     */
    @Override
    public int deleteAssetStockByIds(Long[] ids)
    {
        return assetStockMapper.deleteAssetStockByIds(ids);
    }

    /**
     * 删除盘点任务信息
     * 
     * @param id 盘点任务主键
     * @return 结果
     */
    @Override
    public int deleteAssetStockById(Long id)
    {
        return assetStockMapper.deleteAssetStockById(id);
    }

    /**
     * 提交盘点任务，更新任务状态，同步更新资产信息
     *
     * @param id 盘点任务主键
     * @return 结果
     */
    @Transactional
    public int submitAssetStock(Long id){
        AssetStock assetStock = assetStockMapper.selectAssetStockById(id);
        if(!assetStock.getStatus().equalsIgnoreCase("1")){
            throw new ServiceException("未完成的任务不能提交");
        }
        if(assetStock.getStatus().equalsIgnoreCase("9")){
            throw new ServiceException("不能重复提交");
        }
        int ret = assetStockDetailMapper.countUnStockAsset(id);
        if(ret > 0){
            throw new ServiceException("本次任务还有 "+ret+" 个资产没有盘点");
        }
        assetStock.setStatus("9");
        assetStock.setUpdateBy(SecurityUtils.getUsername());
        assetStock.setUpdateTime(new Date());
        ret = assetStockMapper.updateAssetStock(assetStock);
        ret += assetStockMapper.countAssetStockWorth(assetStock);
        ret += assetStockDetailMapper.copyAssetStockResult(id);
        return ret;
    }
}
